public class Divide {

    public static void main(String[] args) {
        System.out.println(new Divide().divide(-2147483648,2));
    }

    public int divide1(int dividend, int divisor) {
        if (dividend == Integer.MIN_VALUE && divisor == -1) return Integer.MAX_VALUE;
        if (dividend == 0) return 0;
        boolean flag1 = dividend < 0;
        boolean flag2 = divisor > 0;
        long one = Math.abs((long) dividend);
        long two = Math.abs((long) divisor);
        long l = 0;
        long r = one;
        while (l < r) {
            long mid = (l + r) / 2 + 1;
            if (mid * two <= one) {
                l = mid;
            } else r = mid - 1;
        }
        return flag1 ^ flag2 ? (int) l : (int) -l;
    }

    public int divide(int dividend, int divisor) {
        if (divisor == Integer.MIN_VALUE) {
            return dividend == Integer.MIN_VALUE ? 1 : 0;
        }
        if (dividend == Integer.MIN_VALUE) {
            if (divisor == 1) return Integer.MIN_VALUE;
            if (divisor == -1) return Integer.MAX_VALUE;
        }
        if (dividend == 0) return 0;
        int one = dividend > 0 ? -dividend : dividend;
        int two = divisor > 0 ? -divisor : divisor;
        int l = 0;
        int r = dividend == Integer.MIN_VALUE ? Integer.MAX_VALUE : Math.abs(dividend);
        while (l < r) {
            int mid = l + ((r - l) >> 1) + 1;
            Info temp = process(two, mid);
            if (!temp.flag || temp.val < one) {
                r = mid - 1;
            } else l = mid;
        }
        return dividend < 0 ^ divisor < 0 ? -l : l;
    }

    public Info process(int cur, int count) {
        if (count == 1) return new Info(cur, true);
        Info process = process(cur, count >> 1);
        if (!process.flag) return new Info(0, false);
        int temp = process.val;
        if (Integer.MIN_VALUE >> 1 > temp) return new Info(0, false);
        int ans = temp + temp;
        boolean flag = (count >> 1) + (count >> 1) != count;
        if (Integer.MIN_VALUE - ans > cur && flag) return new Info(0, false);
        if (flag) ans += cur;
        return new Info(ans, true);
    }

    private class Info {
        private int val;
        private boolean flag;

        public Info(int val, boolean flag) {
            this.val = val;
            this.flag = flag;
        }
    }
}
